﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;


/// <summary>
/// SQ_Eval 的摘要说明
/// </summary>
public class SQ_Eval
{
    DBHelper db = new DBHelper();
    private DateTime NowKq;
    private string kq_s_d;
    private string kq_e_d;

    private int kp_year = 0;
    private int kp_month = 0;
    public int now_year = 0;
    public int now_month = 0;
    public int last_year = 0;
    public int last_month = 0;
    private string now_kq_s_d;
    private string now_kq_e_d;

    public SQ_Eval()
    {
        string sql = "select * from A_Eval_Sys where [state]=0";
        DataTable dt = db.getList(sql);
        int year = Convert.ToInt32(dt.Rows[0]["year"].ToString());
        kp_year = year;
        int month = Convert.ToInt32(dt.Rows[0]["month"].ToString());
        kp_month = month;

        NowKq = Convert.ToDateTime(year + "-" + month + "-01");
        kq_s_d = dt.Rows[0]["rq_start"].ToString();
        kq_e_d = dt.Rows[0]["rq_end"].ToString();

        DateTime now = DateTime.Now;
        now_year = now.Year;
        now_month = now.Month;
        last_year = now.AddMonths(-1).Year;
        last_month = now.AddMonths(-1).Month;
        now_kq_s_d = kq_s_d;
        now_kq_e_d = kq_e_d;

    }


    public void StartEval(string sign)
    {

        //计算完毕之后记得写入统计
        switch (sign)
        {
            case "0":
                return;
                SetEvalMain();
                SetGdTask();
                SetZdTask();
                SetTaskEvalGp();
                SetSpecial();
                SetZZZZ();
                string sql = "insert into A_Eval_Log (type,logtxt) values('0',N'自动计算考评')";
                db.executeNoneQuery(sql);
                break;
            case "1":
                SetEvalMain();
                break;
            case "2":
                SetGdTask();
                break;
            case "3":
                SetZdTask();
                SetZdTaskPoint();
                break;
            case "4":
                SetTaskEvalGp();
                break;
            case "5":
                SetSpecial();
                break;
            case "6":
                SetZZZZ();
                break;
        }
    }

    #region 自动生成当月考评总表及考评表 
    /// <summary>
    /// (一)自动生成当月考评总表及考评表
    /// </summary>
    private void SetEvalMain()
    {
        DateTime start_day = Convert.ToDateTime(now_kq_s_d);
        DateTime end_day = Convert.ToDateTime(now_kq_e_d);
        string sql = "";
        sql = "select * from dict_dept_position where isUsing=1 and ((LastYear<" + now_year + ") or(LastYear=" + now_year + " and  LastMonth<" + now_month + "))";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            //这里需要调用两个方法 写入总表 写入考评总表 
            string Psid = dr["id"].ToString();
            string UserBh = dr["userbh"].ToString();
            string Evalid = dr["evalid"].ToString();
            int Year = now_year;
            int Month = now_month;
            sql = "insert into A_Eval_Main (psid,userbh,year,month) values ('" + Psid + "','" + UserBh + "','" + Year + "','" + Month + "')";
            db.executeNoneQuery(sql);
            sql = "update dict_dept_position set LastYear=" + now_year + ",LastMonth=" + now_month + " where id=" + dr["id"].ToString();
            db.executeNoneQuery(sql);
            ii++;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(1,N'成功写入" + now_year + "年" + now_month + "月统计记录" + ii + "条')";
        db.executeNoneQuery(sql);

    }

    #endregion

    #region 自动生成当月固定任务

    /// <summary>
    /// 自动生成当月固定任务
    /// </summary>
    private void SetGdTask()
    {
        DateTime start_day = Convert.ToDateTime(now_kq_s_d);
        DateTime end_day = Convert.ToDateTime(now_kq_e_d);
        DateTime mid_day = start_day;
        string sql = "";
        sql = "select * from dict_dept_position_Task where isUsing=1 and ((Year<" + kp_year + ") or(Year=" + kp_year + " and  Month<" + kp_month + "))";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            string id = dr["id"].ToString();

            int sc_d = Convert.ToInt16(dr["sc_d"].ToString());
            if (sc_d > 0 && sc_d <= 28)
            {
                mid_day = start_day.AddDays(sc_d);
            }
            else
            {
                mid_day = end_day;
            }


            sql = "insert into A_Task(type,title,detail,Tdata,Bm_Kh,Ry_Kh,Bm_Bkh,Ry_Bkh,LX_sj,LX_kh,rq_yjs,Year,Month,iszd,LX_fd,RealId) values(";
            sql += "0,N'" + dr["title"].ToString() + "',N'" + dr["detail"].ToString() + "','" + dr["Tdata"].ToString() + "','" + dr["Bm_Kh"].ToString() + "','" + dr["Ry_Kh"].ToString() + "','" + dr["Bm_Bkh"].ToString() + "','" + dr["Ry_Bkh"].ToString() + "','" + dr["LX_sj"].ToString() + "','" + dr["LX_kh"].ToString() + "',N'" + mid_day.ToString("yyyy-MM-dd") + "','" + kp_year + "','" + kp_month + "','" + dr["iszd"].ToString() + "','" + dr["LX_fd"].ToString() + "'," + id + ")";
            int i = db.executeNoneQuery(sql);
            if (i == 1)
            {
                sql = "update dict_dept_position_Task set Year=" + kp_year + ",Month=" + kp_month + " where id=" + id;
                db.executeNoneQuery(sql);
                ii++;
            }

        }

        sql = "insert into A_Eval_Log(type,logtxt) values(2,N'成功写入" + kp_year + "年" + kp_month + "月固定任务" + ii + "条')";
        db.executeNoneQuery(sql);

    }
    #endregion

    #region 计算自动完成任务
    /// <summary>
    /// 计算自动完成任务
    /// </summary>
    private void SetZdTask()
    {
        string now_day = DateTime.Now.ToString("yyyy-MM-dd");

        string sql = "";
        sql = " update A_Task set iszd=0 where LX_sj=1";
        db.executeNoneQuery(sql);

        //自动完成任务
        sql = "update A_Task set state=1,task_state=1,bz_js=N'系统自动完成' where state=0 and iszd=1 and rq_yjs<'" + now_day + "'";
        //超期自动未完成
        sql = "update A_Task set  state=2,task_state=0,bz_js=N'系统自动超期' where state=0  and iszd=0 and rq_yjs<'" + now_day + "'";
        db.executeNoneQuery(sql);
    }
    #endregion

    #region 计算任务分数
    /// <summary>
    /// 计算自动完成任务
    /// </summary>
    private void SetZdTaskPoint()
    {

        //任务未完成
        string sql = "update A_Task set S_FZ=(0-FZ-FZ),K_FZ=0,IS_JS=1 where state=2 and type=0 and IS_JS=0";
        db.executeNoneQuery(sql);
        sql = "update A_Task set S_FZ=(0-FZ),K_FZ=0,IS_JS=1 where state=2 and type=1 and IS_JS=0";
        db.executeNoneQuery(sql);
        sql = "update A_Task set S_FZ=(0-FZ),K_FZ=0,IS_JS=1 where state=2 and type=2 and IS_JS=0";
        db.executeNoneQuery(sql);


        //非考核型任务  任务完成 但  不完美
        sql = "update A_Task set S_FZ=(0-FZ),K_FZ=0,IS_JS=1 where state=1 and type=0 and LX_kh=0 and task_state=0 and IS_JS=0";
        db.executeNoneQuery(sql);

        //考核型任务 任务完成 但不完美
        sql = "update A_Task set S_FZ=0,K_FZ=0-FZ,IS_JS=1 where state=1 and type=0 and LX_kh=1 and task_state=0 and IS_JS=0  and LX_fd=0";
        db.executeNoneQuery(sql);
        sql = "update A_Task set S_FZ=FZ,K_FZ=0-FZ,IS_JS=1 where state=1 and type<>0 and LX_kh=1 and task_state=0 and IS_JS=0  and LX_fd=0";
        db.executeNoneQuery(sql);


        //无论什么任务 任务完成 且很完美
        sql = "update A_Task set S_FZ=0,K_FZ=0,IS_JS=1 where state=1 and type=0 and task_state=1 and IS_JS=0 and LX_fd=0";
        db.executeNoneQuery(sql);
        sql = "update A_Task set S_FZ=FZ,K_FZ=0,IS_JS=1 where state=1 and type=<>0 and task_state=1 and IS_JS=0 and LX_fd=0";
        db.executeNoneQuery(sql);


        try
        {
            sql = "select * from A_Task where Year=" + kp_year + " and Month=" + kp_month + " and state=1 and LX_fd=1";
            DataTable dt = db.getList(sql);
            foreach (DataRow dr in dt.Rows)
            {
                string LX_kh = dr["LX_kh"].ToString();
                string id = dr["id"].ToString();
                double Pdata = Convert.ToDouble(dr["Pdata"].ToString());
                double Tdata = Convert.ToDouble(dr["Tdata"].ToString());
                if (Tdata == 0)
                {
                    Tdata = Pdata;
                }
                double fz = Convert.ToDouble(dr["fz"].ToString());
                double mid = ((Pdata - Tdata) / Tdata) * fz;
                double S_FZ = 0;
                double K_FZ = 0;

                switch (dr["type"].ToString())
                {
                    case "0":
                        if (LX_kh == "True")
                        {
                            S_FZ = 0;
                            K_FZ = fz + mid;
                        }
                        else
                        {
                            S_FZ = fz + mid;
                            K_FZ = 0;
                        }
                        break;
                    case "1":
                        {
                            if (LX_kh == "True")
                            {
                                S_FZ = fz;
                                K_FZ = fz + mid;
                            }
                            else
                            {
                                S_FZ = fz + mid;
                                K_FZ = 0;
                            }
                            break;
                        }
                    case "2":
                        {
                            if (LX_kh == "True")
                            {
                                S_FZ = fz;
                                K_FZ = fz + mid;
                            }
                            else
                            {
                                S_FZ = fz + mid;
                                K_FZ = 0;
                            }
                            break;
                        }
                }
                sql = "update A_Task set S_FZ=" + S_FZ + ",K_FZ=" + K_FZ + " where id=" + id;
                db.executeNoneQuery(sql);
            }
        }
        catch (Exception ex)
        {

        }

    }
    #endregion

    #region 计算当月考评任务得分
    /// <summary>
    /// 计算当月考评任务得分
    /// </summary>
    private void SetTaskEvalGp()
    {
        string sql = "select * from dict_dept_position where isUsing=1 ";
        DataTable dt = db.getList(sql);
        int ii = 0;
        int task_zx_s_0 = 0;
        int task_zx_s_1 = 0;
        int task_zx_s_2 = 0;
        int task_zx_p_0 = 0;
        int task_zx_p_1 = 0;
        int task_zx_p_2 = 0;
        int task_zx_p = 0;


        foreach (DataRow dr in dt.Rows)
        {
            string userbh = dr["userbh"].ToString();
            string psid = dr["id"].ToString();

            //年度任务数
            task_zx_s_0 = db.getCount("v_A_Task", "(Bm_Kh = '" + psid + "' or Ry_Kh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=0") + db.getCount("v_A_Task", "(Bm_Bkh = '" + psid + "' or Ry_Bkh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=0");

            //年度任务得分
            task_zx_p_0 = Convert.ToInt16(db.getList("select ISNULL(sum(S_FZ),0) as S_FZ from v_A_Task where (Bm_Kh = '" + psid + "' or Ry_Kh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=0").Rows[0][0].ToString()) + Convert.ToInt16(db.getList("select ISNULL(sum(K_FZ),0) as S_FZ from v_A_Task where (Bm_Bkh = '" + psid + "' or Ry_Bkh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=0").Rows[0][0].ToString());

            //月度任务数
            task_zx_s_1 = db.getCount("v_A_Task", "(Bm_Kh = '" + psid + "' or Ry_Kh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=1") + db.getCount("v_A_Task", "(Bm_Bkh = '" + psid + "' or Ry_Bkh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=1");

            //月度任务得分
            task_zx_p_1 = Convert.ToInt16(db.getList("select ISNULL(sum(S_FZ),0) as S_FZ from v_A_Task where (Bm_Kh = '" + psid + "' or Ry_Kh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=1").Rows[0][0].ToString()) + Convert.ToInt16(db.getList("select ISNULL(sum(K_FZ),0) as S_FZ from v_A_Task where (Bm_Bkh = '" + psid + "' or Ry_Bkh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=1").Rows[0][0].ToString());

            //临时任务数
            task_zx_s_2 = db.getCount("v_A_Task", "(Bm_Kh = '" + psid + "' or Ry_Kh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=2") + db.getCount("v_A_Task", "(Bm_Bkh = '" + psid + "' or Ry_Bkh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=2");

            //临时任务得分
            task_zx_p_2 = Convert.ToInt16(db.getList("select ISNULL(sum(S_FZ),0) as S_FZ from v_A_Task where (Bm_Kh = '" + psid + "' or Ry_Kh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=2").Rows[0][0].ToString()) + Convert.ToInt16(db.getList("select ISNULL(sum(K_FZ),0) as S_FZ from v_A_Task where (Ry_Bkh = '" + psid + "' or Bm_Bkh='" + psid + "')  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=2").Rows[0][0].ToString());

            //总得分
            task_zx_p = 1000 + task_zx_p_0 + task_zx_p_1 + task_zx_p_2;


            sql = "update A_Eval_Main set task_zx_s_0=" + task_zx_s_0 + ",task_zx_p_0=" + task_zx_p_0 + ",task_zx_s_1=" + task_zx_s_1 + ",task_zx_p_1=" + task_zx_p_1 + ",task_zx_s_2=" + task_zx_s_2 + ",task_zx_p_2=" + task_zx_p_2 + ",task_zx_p=" + task_zx_p + " where psid=" + psid + " and userbh=" + userbh + " and year=" + kp_year + " and month=" + kp_month;
            ii = ii + db.executeNoneQuery(sql);

        }
        sql = "insert into A_Eval_Log(type,logtxt) values(4,N'成功计算" + kp_year + "年" + kp_month + "月任务得分" + ii + "条')";
        db.executeNoneQuery(sql);
    }
    #endregion

    #region 特殊加减分
    /// <summary>
    /// 计算特殊扣分
    /// </summary>
    private void SetSpecial()
    {
        string sql = "update A_Special_Eval set fz=(0-fz) where  isUsing=1 and [year]= " + kp_year + " and [month] = " + kp_month + "  and fz>0 and IsAdd=0";
        db.executeNoneQuery(sql);

        sql = "select sum(fz) as fz,psid from A_Special_Eval where  isUsing=1 and [year]= " + kp_year + " and [month] = " + kp_month + " group by psid";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            string psid = dr["psid"].ToString();
            string fz = dr["fz"].ToString();
            sql = "update A_Eval_Main set special=" + fz + " where psid='" + psid + "' and year=" + kp_year + " and month=" + kp_month;
            db.executeNoneQuery(sql);
            ii++;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(5,N'成功计算" + kp_year + "年" + kp_month + "月特殊考评" + ii + "条')";
        db.executeNoneQuery(sql);
    }
    #endregion

    #region 最终得分
    /// <summary>
    /// 最终得分
    /// </summary>
    private void SetZZZZ()
    {
        string tsql = " update A_Eval_Main set task_zx_p_zz = (task_zx_p + special) where year = '" + kp_year + "' and month = '" + kp_month + "'";
        db.executeNoneQuery(tsql);
    }
    #endregion
}
